import 'package:json_annotation/json_annotation.dart';

import 'message_db.dart';

part 'message_send_model.g.dart';

/// 消息发送模型类
/// 用于封装发送消息时所需的各项参数
/// 支持JSON序列化，便于网络传输
@JsonSerializable()
class MessageSendModel {
  /// 消息类型
  @JsonKey(name: "msg_type")
  String? msgType;

  /// 消息客户端ID
  /// 用于标识消息的唯一性
  @JsonKey(name: "msg_client_id")
  String? msgClientId;

  /// 消息接收者ID
  @JsonKey(name: "msg_receiver_id")
  String? msgReceiverId;

  /// 消息接收者类型
  /// 用于区分接收者是个人还是群组
  @JsonKey(name: "msg_receiver_type")
  int? msgReceiverType;

  /// 消息内容格式
  /// 1: JSON格式
  /// 2: Protobuf格式
  @JsonKey(name: "msg_content_fmt")
  int? msgContentFmt;

  /// 消息发送时间
  @JsonKey(name: "send_time")
  String? sendTime;

  @JsonKey(name: "send_timestamp")
  int? sendTimeStamp;

  String convId = '';

  /// 消息内容类型
  /// 可选值：text, image, audio, video, file等
  /// 支持扩展新的类型
  @JsonKey(name: "msg_content_type")
  String? msgContentType;

  /// 消息内容
  @JsonKey(name: "msg_content")
  String? msgContent;

  /// 默认构造函数
  MessageSendModel();

  /// 命名构造函数
  /// 用于创建包含所有字段的消息发送对象
  MessageSendModel.create({
    this.msgType,
    this.msgClientId,
    this.msgReceiverId,
    this.msgReceiverType,
    this.msgContentFmt,
    this.msgContentType,
    this.msgContent,
    this.sendTime,
    this.convId = '',
    this.sendTimeStamp,
  });

  MessageDb toMessageEntity() {
    return MessageDb(
      convId: convId,
      convType: msgReceiverType,
      msgClientId: msgClientId,
      msgContent: msgContent,
      msgContentFmt: msgContentFmt,
      msgContentType: msgContentType,
      isMyself: true,
      msgId: msgClientId,
      msgReadTime: '',
      msgStatus: 1,
      msgType: msgType,
      sendTimeStamp: sendTimeStamp,
      senderAvatar: '',
      senderId: '',
      senderNick: '',
      receiversAll: '',
      receiversRead: '',
    );
  }

  /// 从JSON映射创建消息发送对象
  factory MessageSendModel.fromJson(Map<String, dynamic> json) => _$MessageSendModelFromJson(json);

  /// 将消息发送对象转换为JSON映射
  Map<String, dynamic> toJson() => _$MessageSendModelToJson(this);
}
